package com.app.controller;

import java.io.File;
import java.util.List;
import java.util.UUID;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.app.entity.*;
import com.app.service.*;

@Controller
@RequestMapping("/question")
public class QuestionController {
    @Resource
    QuestionService questionService;

    @RequestMapping("/question/add")
    public String questionAdd() {
        return "admin/question/add";
    }

    @RequestMapping(value = "/question/save", method = RequestMethod.POST)
    public String questionSave(String short_title,
                               String question_name,
                               String question_category,
                               String score,
                               String step,
                               String created,
                               String status,
                               String userid,
                               String clicked,
                               Model model) {
        Question modelX = new Question();
        modelX.setShort_title(short_title);
        modelX.setQuestion_name(question_name);
        modelX.setQuestion_category(question_category);
        modelX.setScore(score);
        modelX.setStep(step);
        modelX.setCreated(created);
        modelX.setStatus(status);
        modelX.setUserid(userid);
        modelX.setClicked(clicked);

        questionService.insert(modelX);
        model.addAttribute("inpost", true);
        model.addAttribute("message", "Success!");
        return "admin/question/add";
    }

    @RequestMapping("/question/delete")
    public String questionDelete(int id) {
        questionService.delete(id);
        return "redirect:list";
    }
    @Resource
    UsersService usersService;
    @RequestMapping("/question/list")
    public ModelAndView questionList() {
        List<Question> questionList = questionService.getAllQuestion();
        System.out.println(questionList.size());
        for (Question question : questionList) {
			question.setUser(usersService.getUsersById(Integer.parseInt(question.getUserid())).getUsername());
		}
        ModelAndView mav = new ModelAndView("admin/question/list");
        mav.addObject("list", questionList);
        return mav;
    }

    @RequestMapping("/question/edit")
    public String questionEdit(String id, Model model) {
        Question question = questionService.getQuestionById(Integer.parseInt(id));
        model.addAttribute("model", question);
        return "admin/question/edit";
    }

    @RequestMapping(value = "/question/update", method = RequestMethod.POST)
    public String questionUpdate(String short_title,
                                 String question_name,
                                 String question_category,
                                 String score,
                                 String step,
                                 String created,
                                 String status,
                                 String userid,
                                 String clicked,
                                 int id,
                                 Model model) {

        Question modelX = questionService.getQuestionById(id);
        modelX.setShort_title(short_title);
        modelX.setQuestion_name(question_name);
        modelX.setQuestion_category(question_category);
        modelX.setScore(score);
        modelX.setStep(step);
        modelX.setCreated(created);
        modelX.setStatus(status);
        modelX.setUserid(userid);
        modelX.setClicked(clicked);

        questionService.update(modelX);
        model.addAttribute("inpost", true);
        model.addAttribute("model", modelX);
        model.addAttribute("message", "Success");
        return "admin/question/edit";
    }
}
